﻿<%@ WebHandler Language="C#" Class="submitComment" %>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Xml;
using BusinessLogic.ClassFactory;
using Common;
using DataAccess;
using LitJson;
using RichECMS;

public class submitComment : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
        Hashtable result = new Hashtable();

        string strCommentintArticleID = HttpContext.Current.Request["Aid"] ?? "null";
        string strCommentSectionID = HttpContext.Current.Request["Sid"] ?? "null";
        string strCommenttitle = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["title"]??"");
        if (strCommenttitle == "	undefined")
        {
            strCommenttitle = "";
        }
        string strCommentchvSummary = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["summary"]??"");
        if (strCommentchvSummary == "undefined")
        {
            strCommentchvSummary = "";
        }
        string strCommentcontent = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["content"]??"");
        if (strCommentcontent == "undefined")
        {
            strCommentcontent = "";
        }
        int intSectionID =-1;
        int intArticleID=-1;

        if (strCommentcontent.Length == 0)
        {
            result["success"] = false;
            result["error"] = "请填写内容";
            //temp["atitle"] =objComment.DataItem.chvTitle;
            context.Response.Write(
  JsonMapper.ToJson(result));
            return;
        }
        
        if (IsNum(strCommentintArticleID))
        {
            intArticleID = int.Parse(strCommentintArticleID);
        }

        if (IsNum(strCommentSectionID))
        {
            intSectionID = int.Parse(strCommentSectionID);
        }
        using (DBManager objDBManager = new DBManager())
        {
            objDBManager.BeginTransaction();
            try
            {
                CommentFactory objFactory = new CommentFactory(objDBManager);
                BusinessLogic.BusinessRules.Comment objComment = objFactory.NewInstance();
                //插入评论表
                objComment.DataItem.chvSummary = strCommentchvSummary;
                objComment.DataItem.chvContent = strCommentcontent;
                objComment.DataItem.dtmInTime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd HH:mm"));
                objComment.DataItem.chvTitle = strCommenttitle==""? (strCommentcontent.Length > 20 ? strCommentcontent.Remove(19) : strCommentcontent) : strCommenttitle;
                objComment.DataItem.chvAuthor = "匿名用户";
                objComment.DataItem.intSort = 0;
                objComment.DataItem.intArticleID =intArticleID;
                objComment.DataItem.intSectionID =intSectionID;
                objComment.DataItem.intVerify = 0;
                objComment.DataItem.chvIP = Util.IPInfoCollection.IPInfo.GetIPAddress(ref context);
                
                objComment.Insert();            
                //提交
                objDBManager.Commit();

                result["success"] = true;
                //temp["atitle"] =objComment.DataItem.chvTitle;
                objFactory.Dispose();
                context.Response.Write(
      JsonMapper.ToJson(result));
            }
            catch (Exception ex)
            {
                //Alert(ex.ToString().Replace("\r\n", " "));
                objDBManager.Rollback();

                result["success"] = false;
                result["error"] = ex.Message;
                //temp["atitle"] =objComment.DataItem.chvTitle;
                context.Response.Write(
      JsonMapper.ToJson(result));
            }
        }






        /*
         string sctxt = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["sc"]);

         StringBuilder sbWhere = new StringBuilder();
             if (sctxt.Trim() != "")
             {
                 sbWhere.AppendFormat(" and Comment.chvTitle like '%{0}%'", sctxt);
             }

         try
         {
          using (CommentFactory objFactory = new CommentFactory())
                 {
                     StringBuilder strWhere = new StringBuilder();
                     strWhere.Append("select *  from Comment where 1=1 " + sbWhere + " order by intSort desc ,dtmInTime desc,intCommentID desc");
                    
                     strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Comment.* from Comment inner  join ArtSecRelation on Comment.intCommentID = ArtSecRelation.intCommentID and Comment.intState = '" + Request["type"].ToString().Trim().Replace("'","")+ "' " + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Comment.intSort desc ,Comment.dtmInTime desc,Comment.intCommentID desc)t order by intSort asc, dtmInTime asc ,intCommentID asc)m " + " order by intSort asc, dtmInTime asc ,intCommentID asc)mm  order by intSort desc, dtmInTime desc ,intCommentID desc");
                    
                     BusinessLogic.BusinessRules.Comment objComment = objFactory.CreatorByFullSql(strWhere.ToString());
                     if (objComment != null)
                     {
                         List<Hashtable> reslist = new List<System.Collections.Hashtable>();
                          for (int i = 0; i < objComment.Count; i++)
                     {
                         Hashtable temp = new Hashtable();
                         temp["aid"] = objComment.DataItem.intCommentID;
                         temp["atitle"] =objComment.DataItem.chvTitle;
 temp["atime"] =objComment.DataItem.dtmInTime.ToString("MM-dd");
                         reslist.Add(temp);
                     }
                      result["list"] = reslist;
                     }
                    
                 }

             context.Response.Write(
            JsonMapper.ToJson(result));
         }
         catch (Exception error)
         {
         }
         */
    }

    /// <summary>
    /// 检测是否能转化为数字
    /// </summary>
    /// <param name="param">检测字符串</param>
    /// <returns>输出true/false</returns>
    public static bool IsNum(string param)
    {
        const string regEx = @"^[0-9]*$";
        if (System.Text.RegularExpressions.Regex.IsMatch(param, regEx))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}